package com.gtw.modules.api.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.modules.api.service.TbAddressService;
import com.gtw.modules.dao.TbAddressDao;
import com.gtw.modules.dao.TbCityDao;
import com.gtw.modules.dao.TbDistrictDao;
import com.gtw.modules.dao.TbProvinecDao;
import com.gtw.modules.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service("tbaddressservice")
public class TbAddressServiceImpl implements TbAddressService {
    @Autowired
    private TbAddressDao tbAddressDao;
    @Autowired private TbCityDao tbCityDao;
    @Autowired private TbProvinecDao tbProvinecDao;
    @Autowired private TbDistrictDao tbDistrictDao;

    @Override
    public Page<TbAddress> queryPageList(Page<TbAddress> pageUtil, Map<String, Object> map) {
        pageUtil.setRecords(tbAddressDao.queryPageList(pageUtil, map));
        return pageUtil;
    }

    @Override
    public TbAddress queryObject(Integer id) {
        return tbAddressDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(TbAddress address) {
        tbAddressDao.insert(address);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void update(TbAddress address) {
        tbAddressDao.updateByPrimaryKey(address);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void delete(Integer id) {
        tbAddressDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<TbAddress> findbyUid(String uid) {
        return tbAddressDao.findbyUid(uid);
    }

    /**
     * 将地址id转化为地址详情
     *
     * @param addressid
     * @return
     */
    @Override
    public String fromIdToDetail(Integer addressid) {
        TbAddress tbAddress = tbAddressDao.selectByPrimaryKey(addressid);
        TbCity tbCity = tbCityDao.selectByPrimaryKey(tbAddress.getCityid());
        TbDistrict tbDistrict = tbDistrictDao.selectByPrimaryKey(tbAddress.getDistrictid());
        TbProvinec tbProvinec = tbProvinecDao.selectByPrimaryKey(tbAddress.getProvinceid());
        return tbProvinec.getProvincename() + " " + tbCity.getCityname() + " " + tbDistrict.getDistrictname()+" "+tbAddress.getDetails();
    }


}
